﻿using System;
using System.Collections.Generic;

namespace SpiderSync
{
    /// <summary>
    /// Queue for synchronization items
    /// </summary>
    class SyncQueue : List<SyncItem>
    {
        public new void Add(SyncItem item)
        {
            lock (this)
            {
                // Clear when resync queued
                if (item.action == SyncAction.resync)
                {
                    this.Clear();
                }
                // On delete action remove all items with the new path
                else if (item.action == SyncAction.delete)
                {
                    this.RemoveWithPath(item.path);
                }
                else if (item.action == SyncAction.rename)
                {
                    this.RemoveEquals(new SyncItem(SyncAction.copy, item.path));
                }
                // Remove duplicates
                else
                {   
                    this.RemoveEquals(item);
                }

                base.Add(item);
            }
        }

        /// <summary>
        /// Converts a filesystem evento to sync item and adds to the queue
        /// </summary>
        /// <param name="fileSystemEventArgs">FilesystemEventArgs or RenameEventArgs</param>
        public void AddFromFilesystemEvent(object fileSystemEventArgs)
        {
            this.Add(SyncItem.FromFilesystemEvent(fileSystemEventArgs));
        }

        /// <summary>
        /// Remove and return last element of the queue
        /// </summary>
        /// <returns></returns>
        public SyncItem Top()
        {
            lock (this)
            {
                SyncItem item;
                
                if (this.Count > 0)
                { 
                    item = this[0];
                    this.RemoveAt(0);
                }
                else
                {
                    item = null;
                }

                return item;
            }
        }

        /// <summary>
        /// Remove items mathes with the specified item
        /// </summary>
        /// <param name="itemToRemove">Item to remove</param>
        protected void RemoveEquals(SyncItem itemToRemove)
        {
            lock (this)
            {
                for (int i = this.Count - 1; i >= 0 && this[i].action != SyncAction.rename; --i)
                {
                    if (this[i] == itemToRemove)
                    {
                        this.RemoveAt(i);
                    }
                }
            }
        }

        /// <summary>
        /// Remove items with specific path
        /// </summary>
        /// <param name="pathToRemove">Path to remove</param>
        protected void RemoveWithPath(string pathToRemove)
        {
            lock (this)
            {
                for (int i = this.Count - 1; i >= 0 && this[i].action != SyncAction.rename; --i)
                {
                    if (this[i].path == pathToRemove)
                    {
                        this.RemoveAt(i);
                    }
                }
            }
        }
    }
}
